from flask import jsonify, make_response, request, g
from flask_restful import Resource
from sqlalchemy import text

from apps.model.serializeModel import model_to_dict, model_to_list
from apps.settingApi.switchCase.casePrimaryKey import caseGetPrimaryKeyData
from apps.settingApi.switchCase.serializeParameter import getArgsData, getParmData, serializeGetFilter, pageResultData, \
    serializeParData

from apps.settingApi.switchCase.settingCurd import getList, postList, getPk, deletePk, putPk
from apps.settingApi.api.basic.model import basicTypeModel, basicDataModel
from apps.settingApi.utils import getLessees
from comm.dbConnect import get_yun_session
from comm.decorator import login_required
from comm.sqlalchemyConfig import db
from apps.settingApi.api.basic.utils import getList as utilsGetLista,postList as utilsPostList,getpk as utilsGetpk,putPk as utilsPutPk,deletePk as utilsDeletePk


basicTypeViewModel = basicTypeModel
basicDataViewModel = basicDataModel
# 字典操作=========================================================
class basicTypeListView(Resource):
    # 查询用户列表
    @login_required()
    def get(self):
        # 获取所有的参数
        args = request.args
        objData ,err = getList(basicTypeViewModel,args)
        if err!=None:
            return make_response(jsonify({'msg': err}), 202)
        return make_response(jsonify({'msg': '成功','result': objData}), 200)
    @login_required()
    def post(self):
        args, err = getArgsData()
        if err != None:
            return make_response(jsonify({'msg': err, }), 202)
        objData,err = postList(basicTypeViewModel,args)
        if err != None:
            return make_response(jsonify({'msg': err}), 202)
        return make_response(jsonify({'msg': '成功', 'result': objData}), 200)

class basicTypePkView(Resource):
    @login_required()
    def get(self, pk):
        objData, err = getPk(basicTypeViewModel, pk)
        if err != None:
            return make_response(jsonify({'msg': err}), 202)
        return make_response(jsonify({'msg': '成功', 'result': objData}), 200)

    @login_required()
    def put(self, pk):
        # 获取所有参数
        args, err = getArgsData()
        if err != None:
            return make_response(jsonify({'msg': err, }), 202)
        objData, err = putPk(basicTypeViewModel, pk, args)
        if err != None:
            return make_response(jsonify({'msg': err}), 202)
        return make_response(jsonify({'msg': '成功', 'result': objData}), 200)

    @login_required()
    def delete(self, pk):
        try:
            deletedata = basicTypeViewModel.query.filter(basicTypeViewModel.id == pk).with_for_update().first()
            deletedata.status=0
            db.session.commit()
        except Exception as e:
            return make_response(jsonify({'msg': '删除失败' + repr(e)}), 202)
        # 查询个更新后的数据
        result = basicTypeViewModel.query.filter(basicTypeViewModel.id == pk).first()
        if result == None:
            return make_response(jsonify({'msg': '数据不存在'}), 202)
        resultData = model_to_dict(result, [])
        return make_response(jsonify({'msg': '成功', 'result': resultData}), 200)



class basicDeptListView(Resource):
    # 查询用户列表
    @login_required()
    def get(self):
        argsData = getParmData(None)
        argsData = getLessees(argsData)
        argsData['basic_value'] = 'dept'
        objData = utilsGetLista(basicDataViewModel,argsData)
        return make_response(jsonify({'msg': '成功', 'result': objData}), 200)


    @login_required()
    def post(self):
        # 获取所有参数
        args, err = getArgsData()
        if err != None:
            return make_response(jsonify({'msg': err, }), 202)
        args['id'] = caseGetPrimaryKeyData()
        args['basic_value'] = 'dept'
        args['create_user'] = g.user_id
        args = getLessees(args)
        objData,err = utilsPostList(basicDataViewModel,args)
        if err!=None:
            return make_response(jsonify({'msg': err, }), 202)
        return make_response(jsonify({'msg': '成功', 'result': objData}), 200)

class basicDeptPkView(Resource):
    @login_required()
    def get(self, pk):
        args = getParmData(None)
        args = getLessees(args)
        args['basic_value'] = 'dept'
        resultData,err = utilsGetpk(basicDataViewModel,args,pk)
        if err!=None:
            return make_response(jsonify({'msg': err, }), 202)
        return make_response(jsonify({'msg': '成功', 'result': resultData}), 200)

    @login_required()
    def put(self, pk):
        args, err = getArgsData()
        if err != None:
            return make_response(jsonify({'msg': err, }), 202)
        args = getLessees(args)
        args['basic_value'] = 'dept'
        args['update_user'] = g.user_id
        objData,err = utilsPutPk(basicDataViewModel,args,pk)
        if err != None:
            return make_response(jsonify({'msg': err, }), 202)
        return make_response(jsonify({'msg': '成功', 'result': objData}), 200)

    @login_required()
    def delete(self, pk):
        objData, err = deletePk(basicDataViewModel, pk)
        if err != None:
            return make_response(jsonify({'msg': err}), 202)
        args = getLessees({})
        args['basic_value'] = 'dept'
        utilsDeletePk(basicDataViewModel,args,pk)
        return make_response(jsonify({'msg': '成功', 'result': objData}), 200)

class basicSalmListView(Resource):
    # 查询用户列表
    @login_required()
    def get(self):
        argsData = getParmData(None)
        argsData = getLessees(argsData)
        argsData['basic_value'] = 'salm'
        objData = utilsGetLista(basicDataViewModel, argsData)
        return make_response(jsonify({'msg': '成功', 'result': objData}), 200)

    @login_required()
    def post(self):
        # 获取所有参数
        args, err = getArgsData()
        if err != None:
            return make_response(jsonify({'msg': err, }), 202)
        args['id'] = caseGetPrimaryKeyData()
        args['basic_value'] = 'salm'
        args['create_user'] = g.user_id
        args = getLessees(args)
        objData, err = utilsPostList(basicDataViewModel, args)
        if err != None:
            return make_response(jsonify({'msg': err, }), 202)
        return make_response(jsonify({'msg': '成功', 'result': objData}), 200)


class basicSalmPkView(Resource):
    @login_required()
    def get(self, pk):
        args = getParmData(None)
        args = getLessees(args)
        args['basic_value'] = 'salm'
        resultData, err = utilsGetpk(basicDataViewModel, args, pk)
        if err != None:
            return make_response(jsonify({'msg': err, }), 202)
        return make_response(jsonify({'msg': '成功', 'result': resultData}), 200)

    @login_required()
    def put(self, pk):
        args, err = getArgsData()
        if err != None:
            return make_response(jsonify({'msg': err, }), 202)
        args = getLessees(args)
        args['basic_value'] = 'salm'
        args['update_user'] = g.user_id
        objData, err = utilsPutPk(basicDataViewModel, args, pk)
        if err != None:
            return make_response(jsonify({'msg': err, }), 202)
        return make_response(jsonify({'msg': '成功', 'result': objData}), 200)

    @login_required()
    def delete(self, pk):
        objData, err = deletePk(basicDataViewModel, pk)
        if err != None:
            return make_response(jsonify({'msg': err}), 202)
        args = getLessees({})
        args['basic_value'] = 'salm'
        utilsDeletePk(basicDataViewModel, args, pk)
        return make_response(jsonify({'msg': '成功', 'result': objData}), 200)



class basicZcnoListView(Resource):
    # 查询用户列表
    @login_required()
    def get(self):
        argsData = getParmData(None)
        argsData = getLessees(argsData)
        argsData['basic_value'] = 'zcno'
        objData = utilsGetLista(basicDataViewModel, argsData)
        return make_response(jsonify({'msg': '成功', 'result': objData}), 200)

    @login_required()
    def post(self):
        # 获取所有参数
        args, err = getArgsData()
        if err != None:
            return make_response(jsonify({'msg': err, }), 202)
        args['id'] = caseGetPrimaryKeyData()
        args['basic_value'] = 'zcno'
        args['create_user'] = g.user_id
        args = getLessees(args)
        objData, err = utilsPostList(basicDataViewModel, args)
        if err != None:
            return make_response(jsonify({'msg': err, }), 202)
        return make_response(jsonify({'msg': '成功', 'result': objData}), 200)


class basicZcnoPkView(Resource):
    @login_required()
    def get(self, pk):
        args = getParmData(None)
        args = getLessees(args)
        args['basic_value'] = 'zcno'
        resultData, err = utilsGetpk(basicDataViewModel, args, pk)
        if err != None:
            return make_response(jsonify({'msg': err, }), 202)
        return make_response(jsonify({'msg': '成功', 'result': resultData}), 200)

    @login_required()
    def put(self, pk):
        args, err = getArgsData()
        if err != None:
            return make_response(jsonify({'msg': err, }), 202)
        args = getLessees(args)
        args['basic_value'] = 'zcno'
        args['update_user'] = g.user_id
        objData, err = utilsPutPk(basicDataViewModel, args, pk)
        if err != None:
            return make_response(jsonify({'msg': err, }), 202)
        return make_response(jsonify({'msg': '成功', 'result': objData}), 200)

    @login_required()
    def delete(self, pk):
        objData, err = deletePk(basicDataViewModel, pk)
        if err != None:
            return make_response(jsonify({'msg': err}), 202)
        args = getLessees({})
        args['basic_value'] = 'zcno'
        utilsDeletePk(basicDataViewModel, args, pk)
        return make_response(jsonify({'msg': '成功', 'result': objData}), 200)


class basicDbPoolListView(Resource):
    # 查询用户列表
    @login_required()
    def get(self):
        argsData = getParmData(None)
        argsData = getLessees(argsData)
        argsData['basic_value'] = 'dbpool'
        objData = utilsGetLista(basicDataViewModel, argsData)
        return make_response(jsonify({'msg': '成功', 'result': objData}), 200)

    @login_required()
    def post(self):
        # 获取所有参数
        args, err = getArgsData()
        if err != None:
            return make_response(jsonify({'msg': err, }), 202)
        args['id'] = caseGetPrimaryKeyData()
        args['basic_value'] = 'dbpool'
        args['create_user'] = g.user_id
        args = getLessees(args)
        # 校验数据库是否可连接
        dbpool = {
            "DBTYPE": args["str03"],
            "USER": args["label"],
            "HOST": args["str02"],
            "PORT": args["str05"],
            "NAME": args["str04"],
            "PASSWORD": args["str01"],
            "DRIVER": args["str06"],
            "MULTIPLEDB": [],
        }
        # 校验数据库是否能连接成功
        session = get_yun_session(engineName=None, dbpool=dbpool)
        try:
            rest = session.execute(text('select 1'), {})
            data = rest.fetchone()
            print(data)
        except Exception as e:
            session.close()
            return make_response(jsonify({'msg': repr(e), }), 202)

        objData, err = utilsPostList(basicDataViewModel, args)
        if err != None:
            return make_response(jsonify({'msg': err, }), 202)
        return make_response(jsonify({'msg': '成功', 'result': objData}), 200)


class basicDbPoolPkView(Resource):
    @login_required()
    def get(self, pk):
        args = getParmData(None)
        args = getLessees(args)
        args['basic_value'] = 'dbpool'
        resultData, err = utilsGetpk(basicDataViewModel, args, pk)
        if err != None:
            return make_response(jsonify({'msg': err, }), 202)
        return make_response(jsonify({'msg': '成功', 'result': resultData}), 200)

    @login_required()
    def put(self, pk):
        args, err = getArgsData()
        if err != None:
            return make_response(jsonify({'msg': err, }), 202)
        args = getLessees(args)
        args['basic_value'] = 'dbpool'
        args['update_user'] = g.user_id

        # 校验数据库是否可连接
        dbpool = {
            "DBTYPE": args["str03"],
            "USER": args["label"],
            "HOST": args["str02"],
            "PORT": args["str05"],
            "NAME": args["str04"],
            "PASSWORD": args["str01"],
            "DRIVER": args["str06"],
            "MULTIPLEDB": [],
        }
        # 校验数据库是否能连接成功
        session = get_yun_session(engineName=None, dbpool=dbpool)
        try:
            rest = session.execute(text('select 1'),{})
            data = rest.fetchone()
            print(data)
        except Exception as e:
            session.close()
            return make_response(jsonify({'msg': repr(e), }), 202)

        objData, err = utilsPutPk(basicDataViewModel, args, pk)
        if err != None:
            return make_response(jsonify({'msg': err, }), 202)
        return make_response(jsonify({'msg': '成功', 'result': objData}), 200)

    @login_required()
    def delete(self, pk):
        objData, err = deletePk(basicDataViewModel, pk)
        if err != None:
            return make_response(jsonify({'msg': err}), 202)
        args = getLessees({})
        args['basic_value'] = 'dbpool'
        utilsDeletePk(basicDataViewModel, args, pk)
        return make_response(jsonify({'msg': '成功', 'result': objData}), 200)